package com.jumlabs.util.data;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.springframework.data.domain.Page;

public abstract class Pagination {	
    public abstract <T>  Collection<T> getObjectListFrom(int startElement,int lenght,Class<T> entityClass); 
    public abstract  long getNumberOfPages(long numberOfItems,int rows);
    public abstract <T>  long getNumberOfItems(Class<T> entityClass);
    public abstract <T> PaginationInfo<T> getPaginationInfo(int pageNumber,int rows,Class<T> oclass); 
	
    public  int calcFirstItemIndexOfPage(int page, int pageSize, long total) {
        if (total == 0) {
            return 0;
        }
        if (page < 1) {
            page = 1;
        }
        int first = (page - 1) * pageSize;
        if (first >= total) {
            first = (int) (((total - 1) / pageSize) * pageSize);    // first item on the last page
        }
        return first;
    }
    
    public static <T>  Map<Object,Object>  getDataTablesFormattedMap(int iDisplayStart,int iDisplayLength,int sEcho,Page<T> page){
		HashMap<Object, Object> result = new HashMap<Object, Object>();		
		result.put("aaData", page.getContent());
		result.put("sEcho", sEcho);
		result.put("iTotalRecords", String.valueOf(page.getTotalElements()));
		result.put("iTotalDisplayRecords",
				String.valueOf(page.getTotalElements()));
		return result;
	}
    
    public static int calcPage(int displayStart,int displayLength){
    	return (int) Math.ceil(displayStart/ displayLength);
    }
}
